/**
 * @Author : woon
 * @Timestamp : 2016-06-29
 */
var page = sm("do_Page");
var app = sm("do_App");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");
var circleUrl = require("url/circle");
var http = require("helper/http");
var httpSilence = require("helper/httpSilence");
var pubUrl = require("url/pub");
var common = require("common");
var bizzType = require("helper/bizzType");
var openHelper = require("helper/open");
var shareHelper = require("helper/share");
var circleHelper = require("helper/circle");
var userUtil = require("helper/user");
var rootview = ui("$");

var listview = ui("ListView");
var listdata = mm("do_ListData");
listview.bindItems(listdata);//绑定item的数据
var noData = [{"template":2,"labelContent":"该话题还没有评论"}];

var httpUrl4Detail = circleUrl.url.topic.detail;
var httpUrl4CommentList = circleUrl.url.topic.commentList;

var userShieldHelper = require("helper/userShield");

var isUpdateInfo = false;
var topicId = null;//上个页面转过来的参数
var pageNum = 1;//当前页码
var isPraised = 2;//是否关注 1-是，2-否
var praiseCount = 0;//点赞数量
var commentCount = 0;//评论数量
var userList4Praise;
//andriod返回键
page.on("back", function() {
    app.closePage({
    	isCircleTopicUpdate:isUpdateInfo,
    	topicId:topicId,
    	isPraised:isPraised,
    	praiseCount:praiseCount,
    	commentCount:commentCount});
});

//从open的上层页面返回,刷新数据
page.on("result", function(data){
    if (data.comment_add) {
    	bindCommentData4Topic(false);
    	isUpdateInfo = true;
    }
});

var menus=[{"template":0,"menuIcon":"source://image/menu/report.png","menuName":"举报","type":"reportTopic"},
		{"template":0,"menuIcon":"source://image/menu/shield.png","menuName":"屏蔽此人话题","type":"addShield"}];
/****************处理页面-头部-begin*****************/
var headerAout = ui("headerAout");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
//var lableNext = ui(addheader+".labelNext");
//左上角-返回
var headerLeft = ui(addheader+".headerLeft");

headerLeft.on("touch", function() {
	app.closePage({
		isCircleTopicUpdate:isUpdateInfo,
    	topicId:topicId,
    	isPraised:isPraised,
    	praiseCount:praiseCount,
    	commentCount:commentCount});
});

var headerRight = ui(addheader + ".headerRight");
//右上角添加+图标
var imgNext = ui(addheader + ".imgNext");
imgNext.source = "source://image/common/more.png";
imgNext.visible = true;
var header = ui("header");
header.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "话题详情"
})
header.refreshData();

/****************处理页面-头部-end*****************/
var footerAout = ui("footerAout");
var addfooter = footerAout.add("footer", "source://view/public/comment/footer.ui", 0, 0);
page.on("loaded", function(){
	//读取当前页面的传入参数
	topicId = page.getData().topicId;
	getTopicDetailInfo();
});

page.on("sendTextMsg", function(data, e) {
	bindCommentData4Topic(true);
	isUpdateInfo = true;
});

//关注后更新数据
page.on("concern2updateTopic", function(data, e) {
	if(listdata.getCount()>0){
		var oneData = listdata.getOne(0);
		oneData.isConcern = data.isConcern;
		listdata.updateOne(0, oneData);//更新“关注状态”
		listview.refreshItems();
	}
	isUpdateInfo = true;
});

page.on("praise2updateTopic", function(data, e) {
	if(listdata.getCount()>0){
		var oneData = listdata.getOne(0);
		oneData.userList4Praise = data.userList4Praise;//点赞人列表
		oneData.praiseUserList = data.praiseUserList;//点赞人列表string
		userList4Praise=data.userList4Praise;
		isPraisedByMe(data.userList4Praise);//判断我是否已点赞
		if(data.userList4Praise!=null && data.userList4Praise.length>0){
			oneData.isShowPraiseList = true;
			praiseCount = data.userList4Praise.length;//点赞数量
		}else{
			oneData.isShowPraiseList = false;
			praiseCount = 0;//点赞数量
		}
		
		listdata.updateOne(0, oneData);//更新“点赞人”
		listview.refreshItems();
		page.fire("showAllPraise",{userList4Praise:userList4Praise});
	}
	isUpdateInfo = true;
});
page.on("refreshList",function(data){
	listview.refreshItems();
})
//判断我是否已点赞
function isPraisedByMe(userList){
	isPraised = 2;
	if(userUtil.loginToken()==""){
		isPraised = 2;
		return;
	}
	if(userList == null || userList.length <=0){
		isPraised = 2;
		return;
	}
	for(var i in userList){
		if(userList[i].id == userUtil.userId()){
			isPraised = 1;
			break;
		}
	}
}

var shieldUserId;
var canDeleteTopic = false;
//话题详情信息
function getTopicDetailInfo(){
	httpSilence.post(httpUrl4Detail,{id:topicId},function(data){
		var circleTopic = data.circleTopic;
		canDeleteTopic = data.canDeleteTopic;//是否可以删除话题
		userList4Praise=circleTopic.userList4Praise;
		if(canDeleteTopic){
			menus.push({"template":0,"menuIcon":"source://image/menu/del.png","menuName":"删除话题","type":"deleteTopic"});
		}
		shieldUserId=data.circleTopic.creatorId;
		isPraised = data.circleTopic.isPraised;
		if(circleTopic){
			circleTopic.template = 1;//指定模板,加载话题说情
			listdata.removeAll();
			circleTopic.voteHeight=data.voteHeight;
			listdata.addOne(circleTopic);//插入一条数据【话题说情】
			//listview.refreshItems();//刷新item数据(这里不用reflash)
			listview.refreshItems();//刷新item数据
			bindCommentData4Topic(false);
			praiseCount = circleTopic.praiseCount;//点赞数量
			commentCount = circleTopic.commentCount;//评论数量
		}
		var param ={
			bizzId:circleTopic.id,	
			bizzType:bizzType.Level2.CIRCLE_TOPIC,
			isPraised:isPraised,
			shareTitle:circleTopic.content,
			shareContent:circleTopic.content,
			shareId:circleTopic.id,
			shareType:shareHelper.type.CIRCLE_TOPIC
		};
		if(circleTopic.imgItems.length>0){
			param.shareImage=circleTopic.imgItems[0].source;
		}
		page.fire("initPraise", param);
	});
}

//全部刷新数据
function bindCommentData4Topic(isSilence){
	var httpHander = http;
	if(isSilence){
		httpHander = httpSilence;
	}
	pageNum=1;
	httpHander.post(pubUrl.url.comment.list,{bizzId:topicId,bizzType:bizzType.Level2.CIRCLE_TOPIC,page:pageNum},function(data){
		if(data.commentPage){
			commentCount = data.commentPage.content.length;//评论数量
			if(listdata.getCount()>1){
				//清除已经加载了评论,
				listdata.removeRange(1, listdata.getCount()-1);
			}
			if(data.commentPage.content.length){
				listdata.addData(data.commentPage.content);
			}
		}
		listview.refreshItems();//刷新item数据
		page.fire("showAllPraise",{userList4Praise:userList4Praise});
	});
}
//下一页的评论信息
function getNextPageCommentData(){
	pageNum++;
	http.post(pubUrl.url.comment.list,{bizzId:topicId,bizzType:bizzType.Level2.CIRCLE_TOPIC,page:pageNum},function(data){
		if(data.commentPage){
			listdata.addData(data.commentPage.content);
			listview.refreshItems();
		}
	});
}

//下拉列表，刷新数据
listview.on("pull", function(data){
	if (data.state == 2){
		listview.rebound();//复位【一定要做】
	}
});

//上拉列表，翻页数据
listview.on("push", function(data){
	if (data.state == 2){
		getNextPageCommentData();
		listview.rebound();//复位【一定要做】
	}
});

/*
 * 菜单弹出
 */
var popmenu = rootview.add("menupop", "source://view/reuse/menu/menul.ui", 0, 0);
var popmenuf = ui("menupop");
headerRight.on("touch", function() {
	popmenuf.fire("popmenuSJ", menus);
});

popmenuf.on("popmenuItem", function(data) {
	var touchIndex = parseInt(data);
	var actionType = menus[touchIndex].type;
	if(actionType=="reportTopic"){//举报
		popmenuf.fire("popmenuClose");
		openHelper.open("source://view/public/report/main.ui",{"bizzId":topicId,"bizzType":bizzType.Level2.CIRCLE_TOPIC});
	}else if(actionType=="addShield"){//屏蔽此人话题
		popmenuf.fire("popmenuClose");
		userShieldHelper.addShield(shieldUserId,userShieldHelper.type.CIRCLE_TOPIC);
	}else if(actionType=="deleteTopic"){//删除话题
		circleHelper.deleteTopic(topicId, function() {
			nf.toast("删除成功");
			app.closePage({topic_delete:true,topicId:topicId});
		});
	}
});

var addShare = rootview.add("share","source://view/reuse/share.ui", 0, 0);